package com.mxchip.livestar.ui.recentcall

import android.provider.CallLog
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.mxchip.livestar.R
import com.mxchip.livestar.base.RecentCallBean
import com.mxchip.livestar.ui.ItemHolder
import kotlinx.android.synthetic.main.item_recent_call.*

class RecentCallAdapter(private val onClickListener: ((View, RecentCallBean) -> Unit)? = null) : RecyclerView.Adapter<ItemHolder>() {

    var data: List<RecentCallBean> = emptyList()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemHolder {
        val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.item_recent_call, parent, false)
        return ItemHolder(view)
    }

    override fun onBindViewHolder(holder: ItemHolder, position: Int) {

        val (name, number, time, _, callType) = data[position]

        if (TextUtils.isEmpty(name)) {
            holder.user_name.text =  number
            holder.user_name.visibility = View.VISIBLE
            holder.user_number.visibility = View.GONE
        } else {
            holder.user_name.text = name
            holder.user_number.text = number
            holder.user_name.visibility = View.VISIBLE
            holder.user_number.visibility = View.VISIBLE
        }
        if (callType == CallLog.Calls.OUTGOING_TYPE) {
            holder.type_img.visibility = View.VISIBLE
            holder.user_name.setTextColor(ContextCompat.getColor(holder.item_view.context, R.color.color_white))
            holder.user_number.setTextColor(ContextCompat.getColor(holder.item_view.context, R.color.color_white_60))
        } else if (callType == CallLog.Calls.MISSED_TYPE) { //未接
            holder.user_name.setTextColor(ContextCompat.getColor(holder.item_view.context, R.color.color_red))
            holder.user_number.setTextColor(ContextCompat.getColor(holder.item_view.context, R.color.color_red))
            holder.type_img.visibility = View.INVISIBLE
        } else {
            holder.type_img.visibility = View.INVISIBLE
            holder.user_name.setTextColor(ContextCompat.getColor(holder.item_view.context, R.color.color_white))
            holder.user_number.setTextColor(ContextCompat.getColor(holder.item_view.context, R.color.color_white_60))
        }
        holder.time.text = time

        holder.containerView.setOnClickListener {
            onClickListener?.invoke(it, data[position])
        }
    }

    override fun getItemCount(): Int {
        return data.size
    }

}